<?php
class ControllerGaraReceipt extends Controller
{
	private $error = array();
	function __construct() 
	{
		
		$this->load->model("core/module");
		$moduleid = $this->getRoute();
		$this->document->title = $this->model_core_module->getBreadcrumbs($moduleid);
		if($this->user->checkPermission($moduleid)==false)
		{
			$this->response->redirect('?route=page/home');
		}
		
		$this->load->model("gara/car");
		$this->load->model("core/category");
		$this->load->model("core/media");
		$this->load->model("gara/receipt");
   	}
	public function index()
	{
		$this->data['insert'] = $this->url->http('gara/receipt/insert');
		$this->data['delete'] = $this->url->http('gara/receipt/delete');
		
		$this->id='content';
		$this->template="gara/receipt_list.tpl";
		$this->layout="layout/center";
		
		if($this->request->get['opendialog']=='true')
		{
			$this->layout="";
			$this->data['dialog'] = true;
			
		}
		$this->render();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->getForm();		
  	}
	
	public function delete() 
	{
		$listid=$this->request->post['delete'];
		//$listmadonvi=$_POST['delete'];
		
		if(count($listid))
		{
			foreach($listid as $id)
			$this->model_gara_receipt->delete($id);
			$this->data['output'] = "Xóa thành công";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	public function getList() 
	{
		
		
		$this->data['datas'] = array();
		$where = "";
		
		$datasearchlike['carnumber'] = urldecode($this->request->get['carnumber']);
		
		
		$arr = array();
		foreach($datasearchlike as $key => $item)
		{
			if($item !="")
				$arr[] = " AND " . $key ." like '".$item."%'";
		}
		
		
		
		$where = implode("",$arr);
		
		$where .= " Order by id desc";
		$rows = $this->model_gara_receipt->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayoutAjax($total, $limit, $page,"#listmovie");
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('gara/receipt/update&id='.$this->data['datas'][$i]['id']);
			$this->data['datas'][$i]['text_edit'] = "Sửa";
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="gara/receipt_table.tpl";
		
		if($this->request->get['opendialog']=='true')
		{
			$this->layout="";
			$this->data['dialog'] = true;
			
		}
		$this->render();
	}
	
	
	private function getForm()
	{
		$this->data['manufacturer'] = array();
		$this->model_core_category->getTree("manufacturer",$this->data['manufacturer']);
		unset($this->data['manufacturer'][0]);
		
		$id = $this->request->get['id'];
		if($id) 
		{
      		$this->data['item'] = $this->model_gara_receipt->getItem($this->request->get['id']);
			
			
    	}
		
		$this->id='content';
		$this->template='gara/receipt_form.tpl';
		$this->layout="layout/center";
		
		$this->render();
	}
	
	public function save()
	{
		$data = $this->request->post;
		
		
		if($this->validateForm($data))
		{
			
			if($data['code']=="")
			{
				$today = $this->date->getToday();
				$year = $this->date->getYear($today);
				$month = $this->date->getMonth($today);
				$data['code'] = $this->model_gara_receipt->gentCode($year.$month);
			}
			$data['id'] = $this->model_gara_receipt->save($data);
			
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		if($data['customername'] == "")
		{
      		$this->error['customername'] = "Bạn chưa nhập tên khách hàng";
    	}
    	if($data['carnumber'] == "")
		{
      		$this->error['carnumber'] = "Bạn chưa nhập số xe";
    	}
		

		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	public function process()
	{
		$id = $this->request->get['id'];
		$this->data['item'] = $this->model_gara_receipt->getItem($id);
		//Get detai
		$where = " AND receiptid = '".$id."'";
		$this->data['data_detail'] = $this->model_gara_receipt->getReceiptDetailList($where);
		foreach($this->data['data_detail'] as $key => $item)
		{
			
			$this->data['data_detail'][$key]['ProductName'] = $this->document->productName($item['mediaid']);
			
		}
		
		$this->id='content';
		$this->template='gara/receipt_process.tpl';
		$this->layout="layout/center";
		
		$this->render();
	}
	public function saveDetail()
	{
		$data = $this->request->post;
		print_r($data);
		$delnhapkho = $data['delnhapkho'];
		if($delnhapkho)
		{
			$arr_nhapkhoid = split(",",$delnhapkho);
			if(count($arr_nhapkhoid))
			{
				foreach($arr_nhapkhoid as $nhapkhoid)
					$this->model_gara_receipt->deleteReceiptDetail($nhapkhoid);
			}
		}
		//Save chi tiet phieu nhap
		$lydothu = $data['lydothu'];
		$thuphi = $data['thuphi'];
		$tongtien = $data['tongtien'];
		$thanhtoan = $data['thanhtoan'];
		$congno = $data['congno'];
		$songaycongno = $data['songaycongno'];
		
		$nhapkhoid = $data['nhapkhoid'];
		$receiptid = $data['id'];
		$arr_mediaid = $data['mediaid'];
		$arr_code = $data['code'];
		$arr_title = $data['title'];
		$arr_soluong = $data['soluong'];
		$arr_madonvi = $data['dlmadonvi'];
		$arr_giatien = $data['giatien'];
		$arr_giamgia = $data['giamgia'];
		$arr_phantramgiamgia = $data['phantramgiamgia'];
		foreach($arr_mediaid as $i => $mediaid)
		{
			$dl['id'] = $nhapkhoid[$i];
			$dl['receiptid'] = $receiptid;
			$dl['mediaid'] = $mediaid;
			
			$dl['quantity'] = $arr_soluong[$i];
			$dl['unit'] = $this->string->toNumber($arr_madonvi[$i]);
			$dl['price'] = $this->string->toNumber($arr_giatien[$i]);
			$dl['discount'] = $this->string->toNumber($arr_giamgia[$i]);
			$dl['discountpercent'] = $this->string->toNumber($arr_phantramgiamgia[$i]);
			$dl['subtotal'] = $dl['quantity'].$dl['price'];
			$this->model_gara_receipt->saveReceiptDetail($dl);
			//$tongtien += $this->string->toNumber($dl['soluong'])*$this->string->toNumber($dl['giatien']);
			
		}
		//Cap nhat receipt
		$this->model_gara_receipt->updateCol($receiptid,'feename',$lydothu);
		$this->model_gara_receipt->updateCol($receiptid,'fee',$this->string->toNumber($thuphi));
		$this->model_gara_receipt->updateCol($receiptid,'total',$this->string->toNumber($tongtien));
		$this->model_gara_receipt->updateCol($receiptid,'payment',$this->string->toNumber($thanhtoan));
		$this->model_gara_receipt->updateCol($receiptid,'remain',$this->string->toNumber($congno));
		
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
}
?>